package org.example.controller;



import jakarta.annotation.Resource;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    @GetMapping("/test")
    public void test() {
        SendResult sendResult = rocketMQTemplate.syncSend("pers", "我是一个简单的消息");
        // 拿到消息的发送状态
        System.out.println(sendResult.getSendStatus());
        // 拿到消息的id
        System.out.println(sendResult.getMsgId());
    }

    @GetMapping("/syncSend")
    public void syncSend() {
        try {
            Message<String> message= MessageBuilder.withPayload("测试同步消息").build();
            // 同步发送消息
            SendResult sendResult = rocketMQTemplate.syncSend("powernode", message);
            // 打印发送结果
            System.out.println(sendResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @GetMapping("/asyncSend")
    public void asyncSend(){
        //异步消息
        Message<String> message= MessageBuilder.withPayload("测试异步消息").build();
        rocketMQTemplate.asyncSend("powernode",message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("success");
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("error");
            }
        });
    }
    @GetMapping("/oneWay")
    public void oneWay(){
        Message<String> message= MessageBuilder.withPayload("测试单向消息").build();
        rocketMQTemplate.sendOneWay("powernode",message);
    }
    @GetMapping("/delay")
    public void delay() throws Exception {
        // 构建消息对象
        Message<String> message = MessageBuilder.withPayload("我是一个延迟消息").build();
        // 发送一个延时消息，延迟等级为4级，也就是30s后被监听消费
        SendResult sendResult = rocketMQTemplate.syncSend("powernode", message, 2000, 4);
        System.out.println(sendResult.getSendStatus());
    }
    @GetMapping("/order")
    public void order(){
        for (int i = 0; i < 10; i++) {
            Message<String> message = MessageBuilder.withPayload("我是一个顺序消息"+i).build();
            rocketMQTemplate.syncSendOrderly("powernode",message,"order");
        }
    }
    @GetMapping("tag")
    public void tag(){
//        Message<String> 测试TAG = MessageBuilder.withPayload("测试TAG")
//                .setHeader(RocketMQHeaders.TAGS, "tagA").build();
        rocketMQTemplate.syncSend("powernode:tagA","测试tag");
    }
    @GetMapping("keys")
    public void keys(){
        Message<String> build = MessageBuilder.withPayload("测试Key")
                .setHeader(RocketMQHeaders.TOPIC, "powernode")
                .setHeader(RocketMQHeaders.TAGS, "tagA")
                .setHeader(RocketMQHeaders.KEYS, "keyA")
                .build();
        rocketMQTemplate.syncSend("powernode:tagA",build);
    }
}
